# author: alex
# time: 2022/4/26 16:23
"""
    点赞测试
"""
import unittest

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.action_chains import ActionChains

class ThumbUp_test(unittest.TestCase):
    def setUp(self) -> None:
        option = webdriver.ChromeOptions()
        option.binary_location = r'E:\Program Files\Google\Chrome\Application\chrome.exe'
        self.driver = webdriver.Chrome(options=option)
        self.url = 'http://localhost:8082/log-reg'
        self.driver.implicitly_wait(10)
        self.driver.get(self.url)
        self.driver.maximize_window()
        self.login()

    def login(self):
        username = self.driver.find_element(By.CSS_SELECTOR, '#form2 > span:nth-child(2) > input')
        password = self.driver.find_element(By.CSS_SELECTOR, '#form2 > span:nth-child(3) > input')
        username.send_keys('12345678901')
        password.send_keys('123456')
        btn = self.driver.find_element(By.CSS_SELECTOR, '#form2 > button')
        btn.click()
        time.sleep(2)
        canvas = self.driver.find_element(By.CSS_SELECTOR, 'body > canvas:nth-child(1)')
        ActionChains(self.driver).move_to_element(canvas).move_by_offset(-475, -200).pause(2).click().perform()
        time.sleep(3)

    def test_thumbUp(self):
        articles = self.driver.find_elements(By.XPATH, '//*[@id="homePage"]/div[2]/div[@class="arcticle"]')
        flag=True
        for article in articles:
            thumb = article.find_element(By.XPATH, './/div[4]/span[2]')
            text1=thumb.get_attribute('class')
            webdriver.ActionChains(self.driver).move_to_element(thumb).click(thumb).perform()
            time.sleep(2)
            text2=thumb.get_attribute('class')
            if text1==text2:
                flag=False
                self.assertTrue(text2!=text1)
                break
            self.driver.execute_script('window.scrollBy(0,60)')
        if flag:
            self.assertTrue(1==1)

    def tearDown(self) -> None:
        self.driver.close()


if __name__ == '__main__':
    unittest.main()